/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 14-6-6
 * Time: 上午11:54
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var VERSION = '1.0',
        jQuery = $ = require('jquery-1.8.2.min');

    var defaults = {
        activation:"hover",
        keepAlive:false,
        maxWidth:'200px',
        defaultPosition:"top",
        delay:400,
        fadeIn:200,
        fadeOut:200,
        attribute:"hint",
        content:false,
        enter:function(){},
        exit:function(){}
    };

    var tiptip_hint,tiptip_text,tiptip_title;
    var timeout = false;
    function Tip(elem,options){
        this.$elem = $(elem);
        this.options = $.extend({},defaults,options);

        this.init();
    }

    Tip.prototype = {
        init:function(){
            if($("#hint").length <= 0){
                tiptip_hint = $('<div id="hint" class="hint" style="max-width: ' +this.options.maxWidth+';"></div>')
                tiptip_title = $('<div id="hint_title" class="hint_title"></div>');
                tiptip_text = $('<div id="hint_text" class="hint_text"></div>');
                $("body").append(tiptip_hint.append(tiptip_title).append(tiptip_text))
            }else{
                tiptip_hint = $("#hint");
                tiptip_title = $("#hint_title");
                tiptip_text = $("#hint_text");
            }
            this.bindEvent();
        },
        bindEvent:function(){
            var options = this.options,
                that = this;
            this.$elem.each(function(){
                var org_elem = $(this),
                    org_title;
                if(options.content){
                    org_title = options.content;
                }else{
                    org_title = org_elem.data(options.attribute);
                }

                if(org_title != ""){
                    if(!options.content){
                        org_elem.removeAttr(options.attribute);
                    }

                    if(options.activation == "hover"){
                        org_elem.hover(function(){
                            that.active_tiptip(org_elem,org_title);
                        },function(){
                            if(!options.keepAlive){
                                that.deactive_tiptip();
                            }
                        });

                        if(options.keepAlive){
                            tiptip_hint.hover(function(){},function(){
                                that.deactive_tiptip();
                            })
                        }
                    }
                }else if(options.activation == 'focus'){
                    org_elem.focus(function(){
                        that.active_tiptip(org_elem,org_title);
                    }).blur(function(){
                            that.deactive_tiptip();
                        })
                }else if(options.activation == 'click'){
                    org_elem.click(function(){
                        that.active_tiptip(org_elem,org_title);
                        return false;
                    }).hover(function(){},function(){
                            if(!options.keepAlive){
                                that.deactive_tiptip();
                            }
                        });

                    if(options.keepAlive){
                        tiptip_hint.hover(function(){},function(){
                            that.deactive_tiptip();
                        })
                    }
                }
            })
        },
        active_tiptip:function(org_elem,org_title){
            var options = this.options,
                position = options.defaultPosition;

            options.enter.call(this);
            tiptip_text.html(org_title);

            var top = parseInt(org_elem.position().top ),
                left = parseInt(org_elem.position().left),
                org_width = parseInt(org_elem.outerWidth()),
                org_height = parseInt(org_elem.outerHeight());

            var tip_w = tiptip_hint.outerWidth(),
                tip_h = tiptip_hint.outerHeight();
            /*var w_width = $(window).width(),
                w_height = $(window).height(),
                t_scroll = $(window).scrollTop(),
                l_scroll = $(window).scrollLeft();*/
            if (options.defaultPosition == 'top') {
                tiptip_hint.css({
                    top: top - tip_h - 10,
                    left: left
                });
            } else if (options.defaultPosition == 'bottom') {
                tiptip_hint.css({
                    top: top + org_height,
                    left: left
                });
            } else if (options.defaultPosition == 'right') {
                tiptip_hint.css({
                    top: top - 10,
                    left: left + org_width + 10
                });
            } else if (options.defaultPosition == 'left') {
                tiptip_hint.css({
                    top: top - 10,
                    left: left - tip_w - 10
                });
            }

            tiptip_hint.addClass(position);

            timeout =  setTimeout(function(){
                tiptip_hint.stop(true,true).fadeIn(options.fadeIn)
            },options.delay)
        },
        deactive_tiptip:function(){
            var options = this.options;
            options.exit.call(this);
            if(timeout){
                clearTimeout(timeout);
            }
            tiptip_hint.fadeOut(options.fadeOut)
        }
    };
    return Tip;
});